package controller

import (
	"encoding/json"
	"sale-admin/internal/app/web/service/common"
	"sale-admin/internal/app/web/service/robot"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type SalesStatement struct{}

// 新增重要提示
func (SalesStatement) Add(c *gin.Context) {
	var params struct {
		Title   string `json:"title"`
		Content string `json:"content"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	// err := robot.SalesStatement{}.AddNotice(MerchantID, params.Title, params.Content) //(c.Request.Context(), MerchantID, UserID, Username, params.Type, params.Name, params.Content, params.FilePath)
	err := robot.SalesStatement{}.AddNotice(MerchantID, params.Title, params.Content) //(c.Request.Context(), MerchantID, UserID, Username, params.Type, params.Name, params.Content, params.FilePath)
	response.JSON(c, nil, err)
}

// 新增重要提示
func (SalesStatement) Edit(c *gin.Context) {
	var params struct {
		ID      int    `json:"id"`
		Title   string `json:"title"`
		Content string `json:"content"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := robot.SalesStatement{}.EditNotice(MerchantID, params.ID, params.Title, params.Content) //(c.Request.Context(), MerchantID, UserID, Username, params.Type, params.Name, params.Content, params.FilePath)
	response.JSON(c, nil, err)
}

// 新增重要提示
func (SalesStatement) Del(c *gin.Context) {
	var params struct {
		ID int `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := robot.SalesStatement{}.DelNotice(MerchantID, params.ID) //(c.Request.Context(), MerchantID, UserID, Username, params.Type, params.Name, params.Content, params.FilePath)
	response.JSON(c, nil, err)
}

// 页面列表
func (SalesStatement) List(c *gin.Context) {
	var params struct {
		Page  int `json:"page"`
		Limit int `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	data, count, err := robot.SalesStatement{}.ListNotice(
		c.Request.Context(),
		MerchantID,
		params.Page,
		params.Limit,
	)
	response.JSON(c, gin.H{
		"data":  data,
		"count": count,
	}, err)
}

// [安卓] 页面列表
func (SalesStatement) AndriodList(c *gin.Context) {
	MerchantID := c.GetString("merchant_id")
	data, _, err := robot.SalesStatement{}.ListNotice(
		c.Request.Context(),
		MerchantID,
		0,
		0,
	)
	// 转换成首字母大写格式
	data1, _ := common.Marshal(data)
	data2 := []map[string]interface{}{}
	json.Unmarshal(data1, &data2)

	common.JSONOld(c, map[string]interface{}{
		"Data": data2,
	}, err)
}

// 详情
func (SalesStatement) Detail(c *gin.Context) {
	var params struct {
		ID int `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	data, err := robot.SalesStatement{}.DetailNotice(
		c.Request.Context(),
		MerchantID,
		params.ID,
	)
	response.JSON(c, data, err)
}

// 列表排序
func (SalesStatement) Sort(c *gin.Context) {
	var params struct {
		ID   int `json:"id"`
		Sort int `json:"sort"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := robot.SalesStatement{}.SortNotice(
		c.Request.Context(),
		MerchantID,
		params.ID,
		params.Sort,
	)
	response.JSON(c, nil, err)
}
